package com.jb.repair.common;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.jb.repair.F1Application;
import com.jb.repair.R;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wp1988 on 2016/7/28 0028.
 */
public class DialogUtil {
    public static ProgressDialog progressDialog;

    private static final String TAG = "DialogUtil";

    public static void selectOperationDialog(final Context context, final int itemId, final DialogOnResultCallback onResultCallback) {

        final Bundle bundle = new Bundle();
        bundle.putInt("itemId", itemId);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_select_opetation, null);
        TextView tv_check_detail = (TextView) view.findViewById(R.id.tv_check_detail);
        TextView tv_delay = (TextView) view.findViewById(R.id.tv_delay);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tv_complete = (TextView) view.findViewById(R.id.tv_complete);
        final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(view).setInverseBackgroundForced(true).show();
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tv_check_detail://
                        Log.i(TAG, "选择了查看详情");
                        bundle.putInt("select", 0);
                        break;
                    case R.id.tv_delay:
                        Log.i(TAG, "选择了延期");
                        bundle.putInt("select", 1);
                        break;
                    case R.id.tv_cancel:
                        Log.i(TAG, "选择了取消");
                        bundle.putInt("select", 2);
                        break;
                    case R.id.tv_complete:
                        Log.i(TAG, "选择了完工");
                        bundle.putInt("select", 3);
                        break;
                    default:
                        bundle.putInt("select", -1);
                        break;
                }
                onResultCallback.onResult(0, bundle);
                alertDialog.dismiss();
            }
        };
        tv_check_detail.setOnClickListener(listener);
        tv_delay.setOnClickListener(listener);
        tv_cancel.setOnClickListener(listener);
        tv_complete.setOnClickListener(listener);

    }

    /**
     * 显示延期页面
     *
     * @param context
     * @param itemId
     * @param onResultCallback
     */
    public static void popDelayPage(final Context context, final int itemId, final DialogOnResultCallback onResultCallback, String result) {

        final Bundle bundle = new Bundle();
        bundle.putInt("itemId", itemId);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_delay_operation, null);
        TextView tvPersonName = (TextView) view.findViewById(R.id.tv_person_name);
        tvPersonName.setText(F1Application.getUserName() != null ? F1Application.getUserName() : "未知用户");
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
        final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(view).setInverseBackgroundForced(true).show();
        final DatePicker dp = (DatePicker) view.findViewById(R.id.dp_delay_date);
        final EditText et = (EditText) view.findViewById(R.id.et_delay_reason);
        et.setText(result);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.btn_cancel://取消
                        bundle.putInt("select", 0);
                        break;
                    case R.id.btn_confirm://确认
                        bundle.putInt("select", 1);
                        bundle.putString("date", dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth());
                        bundle.putString("reason", et.getText().toString());
                        break;

                }
                onResultCallback.onResult(1, bundle);
                alertDialog.dismiss();
            }
        };
        btnCancel.setOnClickListener(listener);
        btnConfirm.setOnClickListener(listener);
    }


    /**
     * 显示取消页面
     *
     * @param context
     * @param itemId
     * @param onResultCallback
     */
    public static void popCancelPage(final Context context, final int itemId, final DialogOnResultCallback onResultCallback, String result) {

        final Bundle bundle = new Bundle();
        bundle.putInt("itemId", itemId);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_cancel_operation, null);
        TextView tvPersonName = (TextView) view.findViewById(R.id.tv_person_name);
        tvPersonName.setText(F1Application.getUserName() != null ? F1Application.getUserName() : "未知用户");
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
        final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(view).setInverseBackgroundForced(true).show();
        final EditText et = (EditText) view.findViewById(R.id.et_cancel_reason);
        et.setText(result);
        TextView tv = (TextView) view.findViewById(R.id.tv_cancel_time);
        tv.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()).toString());

        final String date = tv.getText().toString();
        Log.i("取消时间为：", date);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.btn_cancel://取消
                        bundle.putInt("select", 0);
                        break;
                    case R.id.btn_confirm://确认
                        bundle.putInt("select", 1);
                        bundle.putString("date", date);
                        bundle.putString("reason", et.getText().toString());
                        break;

                }
                onResultCallback.onResult(2, bundle);
                alertDialog.dismiss();
            }
        };
        btnCancel.setOnClickListener(listener);
        btnConfirm.setOnClickListener(listener);

    }


    /**
     * 显示完工页面
     *
     * @param context
     * @param itemId
     * @param onResultCallback
     */
    public static void popCompletePage(final Context context, final int itemId, final DialogOnResultCallback onResultCallback, String result) {

        final Bundle bundle = new Bundle();
        bundle.putInt("itemId", itemId);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_complete_operation, null);
        TextView tvPersonName = (TextView) view.findViewById(R.id.tv_person_name);
        tvPersonName.setText(F1Application.getUserName() != null ? F1Application.getUserName() : "未知用户");
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
        final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(view).setInverseBackgroundForced(true).show();
        final EditText et = (EditText) view.findViewById(R.id.et_complete_intro);
        et.setText(result);
        TextView tv = (TextView) view.findViewById(R.id.tv_complete_time);
        tv.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()).toString());

        final String date = tv.getText().toString();
        Log.i("完成时间为：", date);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.btn_cancel://取消
                        bundle.putInt("select", 0);
                        break;
                    case R.id.btn_confirm://确认
                        bundle.putInt("select", 1);
                        bundle.putString("date", date);
                        bundle.putString("intro", et.getText().toString());
                        break;

                }
                onResultCallback.onResult(3, bundle);
                alertDialog.dismiss();
            }
        };
        btnCancel.setOnClickListener(listener);
        btnConfirm.setOnClickListener(listener);

    }
}
